package expenager.project;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBCurrentSetting {
	static final String TAG = "DBCurrentSetting";
	static final String DATABASE_NAME = "dbexpenager";
	static final int DATABASE_VERSION = 2;
	
	static final String TABLE_SETTING = "SETTING";	
	static final String SET_SIGN = "sign";
	
	static final String CREATE_SETTING = "CREATE TABLE "+TABLE_SETTING+" ("+SET_SIGN+" VARCHAR);";
	
	final Context context;
	
	DatabaseHelper DBHelper;
	SQLiteDatabase db;
	
	public DBCurrentSetting (Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper (Context context) {
			super(context,DATABASE_NAME,null,DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			try {
				//Create and Insert Setting Table
				ContentValues initialValues = new ContentValues();
				
				db.execSQL(CREATE_SETTING);
		        
				initialValues.put(SET_SIGN, "$");
		        db.insert(TABLE_SETTING, null, initialValues);		        

			}
			catch (SQLException e) {
				e.printStackTrace();
				}
			}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion+", which will destroy all data");
			db.execSQL("DROP TABLE IF EXISTS contacts");
			onCreate(db);
		}
	}
	
	//---opens the database---
    public DBCurrentSetting open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---
    public void close() 
    {
        DBHelper.close();
    }
	
    public void setCurrencySign(String sign) {
    	ContentValues initialValues = new ContentValues();
    	initialValues.put(SET_SIGN, sign);
    	db.update(TABLE_SETTING, initialValues, null, null);
    }
    
    public Cursor getCurrencySign() throws SQLException
    {
        Cursor mCursor = db.query(true, TABLE_SETTING, new String[] {SET_SIGN}, null, null, null, null, null, null);
        if(mCursor!=null){
        	mCursor.moveToFirst();
        }
        return mCursor;
    }
}
